-- Leviathan (c) v.v.balashoff
local Class = require('leviathan/Class')
local fontsList = require('fonts/list')

local FontsManager = Class:derive('FontsManager')

function FontsManager:new()
    self.fonts = {}
end

function FontsManager:getFont(fontPath, size)
    local r = nil
    -- check fonts list
    local f = fontsList[fontPath]
    if not f then
        -- TODO переделать в ошибку?
        print('ERROR! Font not found: '..fontPath)
        os.exit()
    end
    -- get font
    if not self.fonts[f] then
        self.fonts.font = {}
    end
    if not self.fonts.font[size] then
        local font = love.graphics.newFont(f, size)
        if not font then
            -- TODO переделать в ошибку?
            print('ERROR! Font not found: '..f)
        end
        self.fonts.font[size] = font
    end
    r = self.fonts.font[size]
    return r
end

function FontsManager:getTextSize(font, text, wrapLimit)
    local w, h, fh
    fh = font:getHeight()
    w, wrappedText = font:getWrap(text, wrapLimit)
    h = fh * #wrappedText

-- TODO www remove it?
--    s.lines = {}
--    local text = s.text
--    if type(text) == 'string' then
--        s.lines[1] = {}
--        s.lines[1].text = text
--    else
--        for k, v in pairs(text) do
--            k = tonumber(k)
--            s.lines[k] = {}
--            local t = {}
--            t.text = v
--            s.lines[k] = t
--        end
--    end
--    -- calculate with and height to contain all text
--    h = 0
--    for i = 1, #s.lines do
--        local _, lines = s.font:getWrap(s.lines[i].text, s.w)
--        s.lines[i].lines = #lines
--        s.lines[i].height = fh * #lines
--        h = h + s.lines[i].height
--    end 

    return w, h
end

return FontsManager
